package com.hunk.utils;

import java.util.HashMap;
import java.util.Map;

import org.springframework.util.Assert;

import com.hunk.test.UserInfo;

/**
 * @author Dengyx
 *
 */
public class Cache {
	private static Map<String, Object> Cache_Param = new HashMap<String, Object>();
	
	/**
	 * @param key
	 * @param c
	 * @return
	 * @throws ClassCastException
	 */
	public static <T> T getter(String key, Class<T> c) throws ClassCastException {
		Object o = Cache_Param.get(key);
		if (o != null) {
			Assert.isInstanceOf(c, o);
			return c.cast(o);
		} else {
			T _o = Generator.gen(c);
			Cache_Param.put(key, _o);
			return _o;
		}
	}
	
	public static void main(String[] args) {
		UserInfo user = Cache.getter("userInfo", UserInfo.class);
		Print.println(JacksonUtil.getJsonStrFromBean(user));
		user.setCode("123333");
		UserInfo user2 = Cache.getter("userInfo", UserInfo.class);
		Print.println(JacksonUtil.getJsonStrFromBean(user2));
	}
}
